//
//  WD_HomeRecomTableViewCell.m
//  BUDING
//
//  Created by HanWoonYong on 16/3/31.
//  Copyright © 2016年 HanWoonYong. All rights reserved.
//

#import "WD_HomeRecomTableViewCell.h"
#import "WD_RecomCollectionViewCell.h"
#import "WD_RecomEpsModel.h"

@interface WD_HomeRecomTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong)NSString *_id;
@end
@implementation WD_HomeRecomTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.backgroundColor = [UIColor whiteColor];
        [self.collectionView registerClass:[WD_RecomCollectionViewCell class] forCellWithReuseIdentifier:@"recom"];
        [self.contentView addSubview:self.collectionView];
    
        self.changeButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.changeButton setTitle:@"换一批" forState:UIControlStateNormal];
        [self.changeButton addTarget:self action:@selector(changeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        self.changeButton.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.changeButton];

    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.flowLayout.itemSize = CGSizeMake(WIDTH / 2 - 15, HEIGHT / 4);
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    self.changeButton.frame = CGRectMake(0, self.bounds.size.height - 50, self.bounds.size.width, 30);
}

- (void)setRecomModel:(WD_RecomModel *)recomModel {
    _recomModel = recomModel;
    self.dataSource = [NSMutableArray arrayWithArray:recomModel.eps];
    [self.collectionView reloadData];
}

#pragma mark - CollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WD_RecomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"recom" forIndexPath:indexPath];
    WD_RecomEpsModel *recomEpsModel = self.dataSource[indexPath.item];
    cell.recomEpsModel = recomEpsModel;
    
    self.idBlock(recomEpsModel._id);
    return cell;
}

- (void)changeButtonAction:(UIButton *)sender {
    NSLog(@"点我了");
    if (self.ButtonBlock) {
        self.ButtonBlock();
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld", indexPath.item);
    self.numBlock(indexPath.item);
//    self.sectionBlock(indexPath.section);
    
    if (self.CellBlock) {
        self.CellBlock();
    }
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
